package com.winz.cultureapp.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.winz.cultureapp.R;
import com.winz.cultureapp.application.DemoApplication;
import com.winz.cultureapp.base.BaseAppCompatActivity;
import com.winz.cultureapp.constants.Constants;
import com.winz.cultureapp.utils.SharePrefsUtil;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;

public class LaunchActivity extends BaseAppCompatActivity {
//    @BindView(R.id.fl_guanggao)
//    FrameLayout mFl_guanggao;
//    @BindView(R.id.tv_millis)
//    TextView mTv_millis;

    boolean isFirstIn = false;

    private static final int GO_HOME = 1000;
    private static final int GO_GUIDE = 1001;
    private static final int GO_GUANGGAO = 1002;
    private static final int GO_SKIP = 1003;
    // 启动页跳转时间
    private static final long SPLASH_DELAY_MILLIS = 6000;
    private int currentMillis = 0;

    public static final String SHAREDPREFERENCES_NAME = "first_pref";

    private Timer timer;


    /**
     *  Handler:跳转到不同界面
     */
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case GO_HOME:
                    goHome();
                    break;
                case GO_GUIDE:
                    goGuide();
                    break;
//                case GO_GUANGGAO:
//                    mFl_guanggao.setVisibility(View.VISIBLE);
//                    break;
//                case GO_SKIP:
//                    if (mTv_millis != null){
//                        mTv_millis.setText(SPLASH_DELAY_MILLIS/1000 - currentMillis + "");
//                    }
//                    break;

            }
            super.handleMessage(msg);
        }
    };
    private Intent intent;

    @Override
    protected int contentView() {
//        fullScreen(this);
        return R.layout.activity_launch;
    }

    @Override
    protected void initViews() {
        // 读取SharedPreferences中需要的数据
        // 使用SharedPreferences来记录程序的使用次数
        SharedPreferences preferences = getSharedPreferences(SHAREDPREFERENCES_NAME, MODE_PRIVATE);
        // 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
        isFirstIn = preferences.getBoolean("isFirstIn", true);
        // 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
//        mTv_millis.setText(SPLASH_DELAY_MILLIS/2000 + "");

        timer = new Timer(true);
        TimerTask task = new TimerTask() {
            public void run() {
                //每次需要执行的代码放到这里面。
                currentMillis++;
//                       if (currentMillis*1000 >= SPLASH_DELAY_MILLIS/2){
//                           if (currentMillis*1000 == SPLASH_DELAY_MILLIS/2){
//                               // 启动广告页
//                               mHandler.sendEmptyMessage(GO_GUANGGAO);
//                           }
//                           mHandler.sendEmptyMessage(GO_SKIP);
//                       }
                if (currentMillis*1000 == SPLASH_DELAY_MILLIS){
                    if (!isFirstIn) {
                        // 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
                        mHandler.sendEmptyMessage(GO_HOME);
                    } else {
//                        mHandler.sendEmptyMessage(GO_GUIDE);
                        mHandler.sendEmptyMessage(GO_HOME);
                    }
                }
            }
        };
        timer.schedule(task, 0, 1000);
        DemoApplication.getInstance().isLogin = SharePrefsUtil.getInstance().getBoolean(Constants.USER_INFO.IS_LOGIN,false);

    }


    private void goHome() {
        startActivity(IndexActivity.class,true);
    }

    private void goGuide() {
        startActivity(WelcomeActivity.class, true);
    }

//    @OnClick({R.id.fl_guanggao,R.id.ll_skip})
//    public void onClick(View view){
//        switch (view.getId()){
//            case R.id.ll_skip:
//                timer.cancel();
//                if (!isFirstIn) {
//                    // 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
//                    mHandler.sendEmptyMessage(GO_HOME);
//                } else {
//                    mHandler.sendEmptyMessage(GO_GUIDE);
//                }
//                break;
//            case R.id.fl_guanggao:
//                showMsg("暂无广告详情");
//                break;
//        }
//    }

}
